library(ggplot2)
library(tidyverse)
library(rdd)
library(rddensity)
library(rdrobust)
library(xtable)
library(stargazer)
library(lfe)
library(stats)


########################################################################################################
## Table A.1: Summary Statistics

dta <- read.csv("data/rdd-data.csv")


summary <- dta %>% select(num.seats, num.inc, challengers.per.seat,
                          prop.inc.running, avg.inc.voteshare,
                          change.voteshare, prop.inc.win, log.pop,
                          prop.white, prop.black, prop.hispanic, prop.asian,
                          prop.renters, log.med.house, log.med.inc,
                          log.med.rent)

names(summary) <- c("Num. Seats", "Num. Incumbents", "Challengers Per Seat",
                    "Prop. Incumbents Running", "Average Incumbent Vote Share",
                    "Change Incumbent Vote Share", "Prop. Incumbents Winning",
                    "Population (Log)", "Prop. White", "Prop. Black", 
                    "Prop. Hispanic", "Prop. Asian", "Prop. Renters", 
                    "Median House Value (Log)", "Median Income (Log)", 
                    "Median Rent (Log)")

stargazer(summary, type = "text",  out = "output/tableA1.tex",
          table.placement = "htb", label = "tab:summary",
          title = c("\\textbf{Summary Statistics}"))


########################################################################################################
## Table A.2: Tax Increases and City Revenue Within-City

dta <- read.csv("data/finance-panel.csv")

m <- felm(log.total.tax.rev ~ tax.treated + log.pop | 
            city + year | 0 | city, data = dta)

m1 <- felm(log.business.tax.rev ~ business.tax.treated + log.pop | 
             city + year | 0 | city, data = dta)

m2 <- felm(log.property.tax.rev ~ property.tax.treated + log.pop | 
             city + year | 0 | city, data = dta)

m3 <- felm(log.sales.tax.rev ~ sales.tax.treated + log.pop | 
             city + year | 0 | city, data = dta)

m4 <- felm(log.transient.tax.rev ~ hotel.tax.treated + log.pop | 
             city + year | 0 | city, data = dta)

m5 <- felm(log.utility.tax.rev ~ utility.tax.treated + log.pop | 
             city + year | 0 | city, data = dta)



stargazer(m, m1, m2, m3, m4, m5, type = "text", star.cutoffs = c(0.05),
          keep.stat = c("adj.rsq", "n"), 
          out = "output/tableA2.tex",
          table.placement = "tb", label = "tab:finance",
          dep.var.caption = "City Tax Revenue by Source (Log)",
          column.labels = c("Total", "Business", "Sales", "Property", "Hotels", "Utilities"),
          dep.var.labels.include = FALSE, font.size = "small", 
          keep = c("tax.treated", "business.tax.treated", "sales.tax.treated", "property.tax.treated", 
                   "hotel.tax.treated", "utility.tax.treated"),
          covariate.labels = c("Any Tax Increase", "Business Tax Increase", "Property Tax Increase", 
                               "Sales Tax Increase", "Transient Occupancy Tax Increase", "Utility Tax Increase"),
          add.lines = list(c("City + Year FEs", rep("Yes", 6)),
                           c("Mean Outcome", c(round(mean(dta$log.total.tax.rev, na.rm = T), 3),
                                               round(mean(dta$log.business.tax.rev, na.rm = T), 3),
                                               round(mean(dta$log.sales.tax.rev, na.rm = T), 3),
                                               round(mean(dta$log.property.tax.rev, na.rm = T), 3),
                                               round(mean(dta$log.transient.tax.rev, na.rm = T), 3),
                                               round(mean(dta$log.utility.tax.rev, na.rm = T), 3)))))


########################################################################################################
## Figure A.1: Total Number of Single-Seat and Multi-Seat Races by Year

cands <- read.csv("data/candidate-data.csv")

seats <- cands %>% 
  ungroup() %>% 
  filter(!(office %in% c("major", "mayor", "city mayor"))) %>% 
  group_by(year) %>%
  select(multi_raceid, vote.for) %>%
  distinct() %>% 
  summarize(
    single_seat_races = sum(vote.for == 1),
    multiseat_races = sum(vote.for > 1))


seat_counts_long <- seats %>%
  pivot_longer(
    cols = c(single_seat_races, multiseat_races),
    names_to = "race_type",
    values_to = "count"
  )

(p <- ggplot(seat_counts_long, aes(x = factor(year), y = count, fill = race_type)) +
    geom_col(position = "dodge") +
    labs(
      x = "Year",
      y = "Number of Races",
      fill = "Race Type",
    ) +
    scale_fill_discrete(
      name = "Race Type",
      labels = c("Multi Seat Races", "Single Seat Races")) +
    theme_bw(base_family = "serif", base_size = 12) + 
    theme(axis.text.x = element_text(angle = 90, vjust = 0.5),
          legend.position = "bottom"))

ggsave("output/figureA1.pdf", plot = p,
       width = 6, height = 4, units = "in", device = "pdf")


########################################################################################################
## Table A.3: Average Incumbent Vote Share by Number of Seats

cands <- read.csv("data/candidate-data.csv")

cands %>% 
  filter(incumb == "Y") %>%
  mutate(incumb_percent = votes/totvotes) %>% 
  group_by(multi_raceid, year, city, office, vote.for) %>% 
  summarize(
    num_races = n(),
    avg_incumb_voteshare = mean(incumb_percent)) %>% 
  ungroup() %>% 
  group_by(vote.for) %>% 
  summarize(num_races = sum(num_races),
            mean_avg_incumb_voteshare = mean(avg_incumb_voteshare)) -> incumb_voteshare_by_seats

colnames(incumb_voteshare_by_seats) <- c("Number of Seats Up in Contest", "Numer of Races", 
                                         "Average Incumbent Vote Share")


tab <- xtable(
  incumb_voteshare_by_seats,
  caption = "Average Incumbent Vote Share by Number of Seats",
  align = c("c", "c", "c", "c")  # 1st 'c' for table-wide centering (often ignored), then one for each column
)


print(tab, include.rownames = FALSE, caption.placement = "top",
      file = "output/tableA3.tex")



########################################################################################################
## Figure A.2: Characteristics of City Elections Are Balanced

dta <- read.csv("data/rdd-data.csv")

# (a) Number of Seats Up For Election
plot <- rdplot(dta$num.seats, dta$marg, binselect = "qs", # x.lim = c(-.3, .3),
               #    y.lim = c(-10, 10), #x.lim = c(-.4, .4),
               x.label = "Vote Share Margin for Tax Increase", 
               y.label = "Number of Seats Up For Election", 
               col.lines = "black", title = "")

(p <- plot$rdplot + geom_point(aes(x=dta$marg, y = dta$num.seats), alpha = .3) +
    theme_bw(base_family = "serif", base_size = 12))

ggsave("output/figureA2a.pdf", plot = p,
       width = 4, height = 4, units = "in", device = "pdf")


# (b) Number of Candidates Running
plot <- rdplot(dta$num.candidates, dta$marg, binselect = "qs", # x.lim = c(-.3, .3),
               #    y.lim = c(-10, 10), #x.lim = c(-.4, .4),
               x.label = "Vote Share Margin for Tax Increase", 
               y.label = "Number of Candidates Running", 
               col.lines = "black", title = "")

(p <- plot$rdplot + geom_point(aes(x=dta$marg, y = dta$num.candidates), alpha = .3) +
    theme_bw(base_family = "serif", base_size = 12))

ggsave("output/figureA2b.pdf", plot = p,
       width = 4, height = 4, units = "in", device = "pdf")


# (c) Mayoral Race Included in Sample
dta <- read.csv("data/rdd-data.csv")

plot <- rdplot(dta$mayoral.race, dta$marg, binselect = "qs", # x.lim = c(-.3, .3),
               #    y.lim = c(-10, 10), #x.lim = c(-.4, .4),
               x.label = "Vote Share Margin for Tax Increase", 
               y.label = "Probability of Mayoral Race in Next Election", 
               col.lines = "black", title = "")

ggsave("output/figureA2c.pdf", plot = p,
       width = 4, height = 4, units = "in", device = "pdf")



########################################################################################################
## Figure A.3: Sorting Tests by Measure Type

taxes <- read.csv("data/taxes.csv")

# (a) Sales taxes

pdf(file = "output/figureA3a.pdf",
    width = 6, height = 3)

sales <- rddensity(taxes[taxes$reclassification=="Sales Tax",]$marg, c=0)

par(mar = c(5, 5, 2, 2), family = "serif", tck = -.018, las = 1, mgp = c(3, 0.6, 0), cex.axis = 1.5)
DCdensity(taxes[taxes$reclassification=="Sales Tax",]$marg, c=0)
title(cex.lab = 1.5, xlab = "Percent Supporting Tax Increase", ylab = "Density") 
abline(v = 0, lty = 2)

dev.off()


# (b) Business taxes

pdf(file = "output/figureA3b.pdf",
    width = 6, height = 3)

business <- rddensity(taxes[taxes$reclassification=="Business Tax",]$marg, c=0.0)

par(mar = c(5, 5, 2, 2), family = "serif", tck = -.018, las = 1, mgp = c(3, 0.6, 0), cex.axis = 1.5)
DCdensity(taxes[taxes$reclassification=="Business Tax",]$marg, c=0.0)
title(cex.lab = 1.5, xlab = "Percent Supporting Tax Increase", ylab = "Density") 
abline(v = 0, lty = 2)

dev.off()

# (c) Property taxes

pdf(file = "output/figureA3c.pdf",
    width = 6, height = 3)


property <- rddensity(taxes[taxes$reclassification=="Property Tax",]$marg, c=0.0)

par(mar = c(5, 5, 2, 2), family = "serif", tck = -.018, las = 1, mgp = c(3, 0.6, 0), cex.axis = 1.5)
DCdensity(taxes[taxes$reclassification=="Property Tax",]$marg, c=0.0)
title(cex.lab = 1.5, xlab = "Percent Supporting Tax Increase", ylab = "Density") 
abline(v = 0, lty = 2)

dev.off()

# (d) Transient occupancy taxes

pdf(file = "output/figureA3d.pdf",
    width = 6, height = 3)


transient <- rddensity(taxes[taxes$reclassification=="Transient Occupancy Tax",]$marg, c=0.0)

par(mar = c(5, 5, 2, 2), family = "serif", tck = -.018, las = 1, mgp = c(3, 0.6, 0), cex.axis = 1.5)
DCdensity(taxes[taxes$reclassification=="Transient Occupancy Tax",]$marg, c=0.0)
title(cex.lab = 1.5, xlab = "Percent Supporting Tax Increase", ylab = "Density") 
abline(v = 0, lty = 2)

dev.off()

########################################################################################################
## Table A.4: Pre-Treatment City Covariates Are Balanced

taxes <- read.csv("data/taxes.csv")
finances <- read.csv("data/finance-panel.csv")
chambers <- read.csv("data/chambers-of-commerce.csv")

finances <- finances %>% select(city, year, log.business.tax.rev, log.property.tax.rev, 
                                log.sales.tax.rev, log.transient.tax.rev, log.utility.tax.rev, 
                                log.total.tax.rev)

taxes <- merge(taxes, finances, by = c("city", "year"), all.x = TRUE)
taxes$city <- tolower(taxes$city)
taxes <- merge(taxes, chambers, by = c("city"), all.x = TRUE)

taxes$own.chamber <- ifelse(taxes$chamber_type == "city", 1, 0)


m <- (rdrobust(taxes$log.pop, taxes$marg, c=0, all=T,
               cluster = taxes$city))

m1 <- (rdrobust(taxes$prop.white, taxes$marg, c=0, all=T,
                cluster = taxes$city))

m2 <- (rdrobust(taxes$prop.black, taxes$marg, c=0, all=T,
                cluster = taxes$city))

m3 <- (rdrobust(taxes$prop.hispanic, taxes$marg, c=0, all=T,
                cluster = taxes$city))

m4 <- (rdrobust(taxes$prop.asian, taxes$marg, c=0, all=T,
                cluster = taxes$city))

m5 <- (rdrobust(taxes$prop.renters, taxes$marg, c=0, all=T,
                cluster = taxes$city)) 

m6 <- (rdrobust(taxes$log.med.house, taxes$marg, c=0, all=T,
                cluster = taxes$city))

m7 <- (rdrobust(taxes$log.med.inc, taxes$marg, c=0, all=T,
                cluster = taxes$city))

m8 <- (rdrobust(taxes$log.med.rent, taxes$marg, c=0, all=T,
                cluster = taxes$city))

m9 <- (rdrobust(taxes$log.business.tax.rev, taxes$marg, c=0, all=T,
                cluster = taxes$city))

m10 <- (rdrobust(taxes$log.property.tax.rev, taxes$marg, c=0, all=T,
                 cluster = taxes$city))

m11 <- (rdrobust(taxes$log.sales.tax.rev, taxes$marg, c=0, all=T,
                 cluster = taxes$city))

m12 <- (rdrobust(taxes$log.transient.tax.rev, taxes$marg, c=0, all=T,
                 cluster = taxes$city))

m13 <- (rdrobust(taxes$log.utility.tax.rev, taxes$marg, c=0, all=T,
                 cluster = taxes$city))

m14 <- (rdrobust(taxes$log.total.tax.rev, taxes$marg, c=0, all=T,
                 cluster = taxes$city))

m15 <- (rdrobust(taxes$own.chamber, taxes$marg, c=0, all=T,
                 cluster = taxes$city))


coef <- (c(m$coef[2], m1$coef[2], m2$coef[2], m3$coef[2], 
           m4$coef[2], m5$coef[2], m6$coef[2],m7$coef[2],
           m8$coef[2], m9$coef[2], m10$coef[2],m11$coef[2],
           m12$coef[2], m13$coef[2], m14$coef[2], m15$coef[2]))

se <- c(paste0("(",round(m$se[2], 3),")"), 
        paste0("(",round(m1$se[2], 3),")"), 
        paste0("(",round(m2$se[2], 3),")"), 
        paste0("(",round(m3$se[2], 3),")"), 
        paste0("(",round(m4$se[2], 3),")"), 
        paste0("(",round(m5$se[2], 3),")"),
        paste0("(",round(m6$se[2], 3),")"),
        paste0("(",round(m7$se[2], 3),")"),
        paste0("(",round(m8$se[2], 3),")"),
        paste0("(",round(m9$se[2], 3),")"),
        paste0("(",round(m10$se[2], 3),")"),
        paste0("(",round(m11$se[2], 3),")"),
        paste0("(",round(m12$se[2], 3),")"),
        paste0("(",round(m13$se[2], 3),")"),
        paste0("(",round(m14$se[2], 3),")"),
        paste0("(",round(m15$se[2], 3),")"))

p <- c(m$pv[2], m1$pv[2], m2$pv[2], m3$pv[2], 
       m4$pv[2], m5$pv[2], m6$pv[2], m7$pv[2],
       m8$pv[2], m9$pv[2], m10$pv[2], m11$pv[2],
       m12$pv[2], m13$pv[2], m14$pv[2], m15$pv[2])

p_adjusted <- p.adjust(p, method = "BH")

bandwidth <- round(c(m$bws[1], m1$bws[1], m2$bws[1], m3$bws[1], m4$bws[1], m5$bws[1],
                     m6$bws[1], m7$bws[1], m8$bws[1], m9$bws[1], m10$bws[1], m11$bws[1],
                     m12$bws[1], m13$bws[1], m14$bws[1], m15$bws[1]), 3)


tab <- data.frame(coef = coef, se = se, p = p, p_adj = p_adjusted, bandwidth = bandwidth)

colnames(tab) <- c("Coef", "SE", "P", "P Adj.", "Bandwidth")
rownames(tab) <- c("Population (Log)", "Prop. White", "Prop. Black", "Prop. Hispanic", "Prop. Asian",
                   "Prop. Renters", "Median House Value (Log)", "Median Income (Log)",
                   "Median Rent (Log)", "Business Tax Revenue (Log)", "Property Tax Revenue (Log)",
                   "Sales Tax Revenue (Log)", "Utility Tax Revenue (Log)", "Hotel Tax Revenue (Log)",
                   "Total Tax Revenue (Log)", "City Chamber of Commerce")

print(xtable(tab, align = c("r", "c", "c", "c", "c", "c")), floating = FALSE, 
      file = "output/tableA4.tex")


########################################################################################################
## Table A.5: No Change in Incumbent or Challenger Behavior After Tax Increase

dta <- read.csv("data/rdd-data.csv")

m <- (rdrobust(dta$prop.running.change, dta$marg, c=0,
               cluster = dta$city))

m1 <- (rdrobust(dta$prop.running.change, dta$marg, c=0, all=T,  p=2,
                cluster = dta$city))

m2 <- (rdrobust(dta$prop.running.change, dta$marg, c=0, all=T,  p=3,
                cluster = dta$city))

m3 <- (rdrobust(dta$challengers.change, dta$marg, c=0, all=T,
                cluster = dta$city))

m4 <- (rdrobust(dta$challengers.change, dta$marg, c=0, all=T,  p=2,
                cluster = dta$city))

m5 <- (rdrobust(dta$challengers.change, dta$marg, c=0, all=T,  p=3,
                cluster = dta$city))


coef <- round(c(m$coef[2], m1$coef[2], m2$coef[2], m3$coef[2], m4$coef[2], m5$coef[2]), 3)

se <- c(paste0("(",round(m$se[2], 3),")"), 
        paste0("(",round(m1$se[2], 3),")"), 
        paste0("(",round(m2$se[2], 3),")"), 
        paste0("(",round(m3$se[2], 3),")"), 
        paste0("(",round(m4$se[2], 3),")"), 
        paste0("(",round(m5$se[2], 3),")"))

p <- round(c(m$pv[2], m1$pv[2], m2$pv[2], m3$pv[2], m4$pv[2], m5$pv[2]), 2)


bandwidth <- round(c(m$bws[1], m1$bws[1], m2$bws[1], m3$bws[1], m4$bws[1], m5$bws[1]), 3)


obs <- c(sum(m$N), sum(m1$N), sum(m2$N))
eff.obs <- c(sum(m$N_h), sum(m1$N_h), sum(m2$N_h), sum(m3$N_h), sum(m4$N_h), sum(m5$N_h))

means <- round(c(mean(dta$prop.running.change, na.rm = T), 
                 mean(dta$prop.running.change, na.rm = T),
                 mean(dta$prop.running.change, na.rm = T),
                 mean(dta$challengers.change, na.rm = T), 
                 mean(dta$challengers.change, na.rm = T), 
                 mean(dta$challengers.change, na.rm = T)), 2)

poly <- c(1, 2, 3, 1, 2, 3)


tab <- rbind(coef, SE = se, poly = poly, bandwidth = bandwidth, 
             obs = obs, eff.obs = eff.obs, means = means)


columns <- c("(1)", "(2)", "(3)", "(4)", "(5)", "(6)")
colnames(tab) <- columns

rownames(tab) <- c("Tax Increase Approved", "", "Polynomial", "Bandwidth", 
                   "Observations", "Obs. in Bandwidth", "Mean Outcome")

tab <- xtable(tab, align = c("l", "c", "c", "c", "c", "c", "c"))


addtorow <- list()
addtorow$pos <- list(-1,0, 2,nrow(tab))
addtorow$command <- c("\\\\[-1.8ex] \\hline \\hline \\\\[-1.8ex]  & 
                      \\multicolumn{3}{c}{$\\Delta$ Incumbents Running} &
                      \\multicolumn{3}{c}{$\\Delta$ Challengers Per Seat} 
                      \\\\ \\cline{2-4} \\cline{5-7} \\\\[-5ex] \\\\",
                      "\\hline \\\\[-1.8ex] ", "\\\\[-1.8ex] \\hline \\\\[-1.8ex]", 
                      "\\\\[-1.8ex] \\hline \\hline \\\\[-1.8ex]")

print(tab, add.to.row = addtorow, hline.after = NULL,
      floating = FALSE, file = "output/tableA5.tex")



########################################################################################################
## Table A.6: Adjusting for Multiple Hypothesis Testing

dta <- read.csv("data/rdd-data.csv")

m <- (rdrobust(dta$avg.inc.voteshare, dta$marg, c=0, all=T, cluster = dta$city, p = 1))

business <- dta[dta$reclassification =="Business Tax",]
m1 <- (rdrobust(business$avg.inc.voteshare, business$marg, c=0, 
                all=T, cluster = business$city, p = 1))

property <- dta[dta$reclassification=="Property Tax",]

m2 <- (rdrobust(property$avg.inc.voteshare, property$marg, c=0, 
                all=T, cluster = property$city, p = 1))

sales <- dta[dta$reclassification=="Sales Tax",]
m3 <- (rdrobust(sales$avg.inc.voteshare, sales$marg, c=0, all=T, 
                cluster = sales$city, p = 1))

utility <- dta[dta$reclassification=="Utility Tax",]
m4 <- (rdrobust(utility$avg.inc.voteshare, utility$marg, c=0, 
                all=T, cluster = utility$city, p = 1))

hotel <- dta[dta$reclassification=="Transient Occupancy Tax",]
m5 <- (rdrobust(hotel$avg.inc.voteshare, hotel$marg, c=0, all=T, cluster = hotel$city, p = 1))



coef <- (c(m$coef[2], m1$coef[2], m2$coef[2], m3$coef[2], 
           m4$coef[2], m5$coef[2]))

se <- c(paste0("(",round(m$se[2], 3),")"), 
        paste0("(",round(m1$se[2], 3),")"), 
        paste0("(",round(m2$se[2], 3),")"), 
        paste0("(",round(m3$se[2], 3),")"), 
        paste0("(",round(m4$se[2], 3),")"), 
        paste0("(",round(m5$se[2], 3),")"))

p <- c(m$pv[2], m1$pv[2], m2$pv[2], m3$pv[2], 
       m4$pv[2], m5$pv[2])

p_adjusted <- p.adjust(p, method = "BH")

bandwidth <- round(c(m$bws[1], m1$bws[1], m2$bws[1], m3$bws[1], m4$bws[1], m5$bws[1]), 3)


tab <- data.frame(coef = coef, se = se, p = p, p_adj = p_adjusted, bandwidth = bandwidth)

colnames(tab) <- c("Coef", "SE", "P", "P Adj.", "Bandwidth")
rownames(tab) <- c("Any Tax Increase", "Business Tax Increase", "Property Tax Increase",
                   "Sales Tax Increase", "Utility Tax Increase", "Hotel Tax Increase")

print(tab, add.to.row = addtorow, hline.after = NULL,
      floating = FALSE, file = "output/tableA6.tex")



########################################################################################################
## Table A.7: Pre-Treatment City Covariates: Business Tax Increases

taxes <- read.csv("data/taxes.csv")
finances <- read.csv("data/finance-panel.csv")
chambers <- read.csv("data/chambers-of-commerce.csv")

finances <- finances %>% select(city, year, log.business.tax.rev, log.property.tax.rev, 
                                log.sales.tax.rev, log.transient.tax.rev, log.utility.tax.rev, 
                                log.total.tax.rev)

taxes <- merge(taxes, finances, by = c("city", "year"), all.x = TRUE)
taxes$city <- tolower(taxes$city)
taxes <- merge(taxes, chambers, by = c("city"), all.x = TRUE)

taxes$own.chamber <- ifelse(taxes$chamber_type == "city", 1, 0)


business <- taxes[taxes$reclassification=="Business Tax",]


m <- (rdrobust(business$log.pop, business$marg, c=0, all=T,
               cluster = business$city))

m1 <- (rdrobust(business$prop.white, business$marg, c=0, all=T,
                cluster = business$city))

m2 <- (rdrobust(business$prop.black, business$marg, c=0, all=T,
                cluster = business$city))

m3 <- (rdrobust(business$prop.hispanic, business$marg, c=0, all=T,
                cluster = business$city))

m4 <- (rdrobust(business$prop.asian, business$marg, c=0, all=T,
                cluster = business$city))

m5 <- (rdrobust(business$prop.renters, business$marg, c=0, all=T,
                cluster = business$city)) 

m6 <- (rdrobust(business$log.med.house, business$marg, c=0, all=T,
                cluster = business$city))

m7 <- (rdrobust(business$log.med.inc, business$marg, c=0, all=T,
                cluster = business$city))

m8 <- (rdrobust(business$log.med.rent, business$marg, c=0, all=T,
                cluster = business$city))

m9 <- (rdrobust(business$log.business.tax.rev, business$marg, c=0, all=T,
                cluster = business$city))

m10 <- (rdrobust(business$log.property.tax.rev, business$marg, c=0, all=T,
                 cluster = business$city))

m11 <- (rdrobust(business$log.sales.tax.rev, business$marg, c=0, all=T,
                 cluster = business$city))

m12 <- (rdrobust(business$log.transient.tax.rev, business$marg, c=0, all=T,
                 cluster = business$city))

m13 <- (rdrobust(business$log.utility.tax.rev, business$marg, c=0, all=T,
                 cluster = business$city))

m14 <- (rdrobust(business$log.total.tax.rev, business$marg, c=0, all=T,
                 cluster = business$city))

m15 <- (rdrobust(business$own.chamber, business$marg, c=0, all=T,
                 cluster = business$city))


coef <- (c(m$coef[2], m1$coef[2], m2$coef[2], m3$coef[2], 
           m4$coef[2], m5$coef[2], m6$coef[2],m7$coef[2],
           m8$coef[2], m9$coef[2], m10$coef[2],m11$coef[2],
           m12$coef[2], m13$coef[2], m14$coef[2], m15$coef[2]))

se <- c(paste0("(",round(m$se[2], 3),")"), 
        paste0("(",round(m1$se[2], 3),")"), 
        paste0("(",round(m2$se[2], 3),")"), 
        paste0("(",round(m3$se[2], 3),")"), 
        paste0("(",round(m4$se[2], 3),")"), 
        paste0("(",round(m5$se[2], 3),")"),
        paste0("(",round(m6$se[2], 3),")"),
        paste0("(",round(m7$se[2], 3),")"),
        paste0("(",round(m8$se[2], 3),")"),
        paste0("(",round(m9$se[2], 3),")"),
        paste0("(",round(m10$se[2], 3),")"),
        paste0("(",round(m11$se[2], 3),")"),
        paste0("(",round(m12$se[2], 3),")"),
        paste0("(",round(m13$se[2], 3),")"),
        paste0("(",round(m14$se[2], 3),")"),
        paste0("(",round(m15$se[2], 3),")"))

p <- c(m$pv[2], m1$pv[2], m2$pv[2], m3$pv[2], 
       m4$pv[2], m5$pv[2], m6$pv[2], m7$pv[2],
       m8$pv[2], m9$pv[2], m10$pv[2], m11$pv[2],
       m12$pv[2], m13$pv[2], m14$pv[2], m15$pv[2])

p_adjusted <- p.adjust(p, method = "BH")

bandwidth <- round(c(m$bws[1], m1$bws[1], m2$bws[1], m3$bws[1], m4$bws[1], m5$bws[1],
                     m6$bws[1], m7$bws[1], m8$bws[1], m9$bws[1], m10$bws[1], m11$bws[1],
                     m12$bws[1], m13$bws[1], m14$bws[1], m15$bws[1]), 3)


tab <- data.frame(coef = coef, se = se, p = p, p_adj = p_adjusted, bandwidth = bandwidth)

colnames(tab) <- c("Coef", "SE", "P", "P Adj.", "Bandwidth")
rownames(tab) <- c("Population (Log)", "Prop. White", "Prop. Black", "Prop. Hispanic", "Prop. Asian",
                   "Prop. Renters", "Median House Value (Log)", "Median Income (Log)",
                   "Median Rent (Log)", "Business Tax Revenue (Log)", "Property Tax Revenue (Log)",
                   "Sales Tax Revenue (Log)", "Utility Tax Revenue (Log)", "Hotel Tax Revenue (Log)",
                   "Total Tax Revenue (Log)", "City Chamber of Commerce")

print(tab, add.to.row = addtorow, hline.after = NULL,
      floating = FALSE, file = "output/tableA7.tex")



########################################################################################################
# Table A.8: Effects of Business Taxes on Incumbent Vote Share: Adjusting for Population

dta <- read.csv("data/rdd-data.csv")
business <- dta[dta$reclassification == "Business Tax", ]

m <- (rdrobust(business$avg.inc.voteshare, business$marg, c=0, all=T, p=1,
               cluster = business$city, covs = business$log.pop))

m1 <- (rdrobust(business$avg.inc.voteshare, business$marg, c=0, all=T, p=2,
                cluster = business$city, covs = business$log.pop))

m2 <- (rdrobust(business$avg.inc.voteshare, business$marg, c=0, all=T, p=3,
                cluster = business$city, covs = business$log.pop))


coef <- round(c(m$coef[2], m1$coef[2], m2$coef[2]), 3)

se <- c(paste0("(",round(m$se[2], 3),")"), 
        paste0("(",round(m1$se[2], 3),")"), 
        paste0("(",round(m2$se[2],3),")"))

p <- round(c(m$pv[2], m1$pv[2], m2$pv[2]), 2)

bandwidth <- round(c(m$bws[1], m1$bws[1], m2$bws[1]), 3)


obs <- c(sum(m$N), sum(m1$N), sum(m2$N))
eff.obs <- c(sum(m$N_h), sum(m1$N_h), sum(m2$N_h))

means <- round(c(mean(business$avg.inc.voteshare, na.rm = T), 
                 mean(business$avg.inc.voteshare, na.rm = T), 
                 mean(business$avg.inc.voteshare, na.rm = T)), 2)

poly <- c(1, 2, 3)


tab <- rbind(coef, SE = se, poly = poly, bandwidth = bandwidth, 
             obs = obs, eff.obs = eff.obs, means = means)


columns <- c("(1)", "(2)", "(3)")

colnames(tab) <- columns

rownames(tab) <- c("Business Tax Approved", "", "Polynomial", 
                   "Bandwidth", "Observations", "Obs. in Bandwidth", "Mean Outcome")

tab <- xtable(tab, align = c("l", "c", "c", "c"))


addtorow <- list()
addtorow$pos <- list(-1,0, 2,nrow(tab))
addtorow$command <- c("\\\\[-1.8ex] \\hline \\hline \\\\[-1.8ex]  & 
                      \\multicolumn{3}{c}{Incumbent Vote Share} 
                      \\\\ \\cline{2-4} \\\\[-5ex] \\\\",
                      "\\hline \\\\[-1.8ex] ", "\\\\[-1.8ex] \\hline \\\\[-1.8ex]", 
                      "\\\\[-1.8ex] \\hline \\hline \\\\[-1.8ex]")

print(tab, add.to.row = addtorow, hline.after = NULL,
      floating = FALSE, file = "output/tableA8.tex")



########################################################################################################
# Table A.9: Effects of Business Tax Increases Not Driven by Cannabis Taxes

dta <- read.csv("data/rdd-data.csv")
business <- dta[dta$reclassification == "Business Tax" & dta$cannabis==0, ]

m <- (rdrobust(business$avg.inc.voteshare, business$marg, c=0, all=T, p=1,
               cluster = business$city))

m1 <- (rdrobust(business$avg.inc.voteshare, business$marg, c=0, all=T, p=2,
                cluster = business$city))

m2 <- (rdrobust(business$avg.inc.voteshare, business$marg, c=0, all=T, p=3,
                cluster = business$city))


coef <- round(c(m$coef[2], m1$coef[2], m2$coef[2]), 3)

se <- c(paste0("(",round(m$se[2], 3),")"), 
        paste0("(",round(m1$se[2], 3),")"), 
        paste0("(",round(m2$se[2],3),")"))

p <- round(c(m$pv[2], m1$pv[2], m2$pv[2]), 2)

bandwidth <- round(c(m$bws[1], m1$bws[1], m2$bws[1]), 3)


obs <- c(sum(m$N), sum(m1$N), sum(m2$N))
eff.obs <- c(sum(m$N_h), sum(m1$N_h), sum(m2$N_h))

means <- round(c(mean(business$avg.inc.voteshare, na.rm = T), 
                 mean(business$avg.inc.voteshare, na.rm = T), 
                 mean(business$avg.inc.voteshare, na.rm = T)), 2)

poly <- c(1, 2, 3)


tab <- rbind(coef, SE = se, poly = poly, bandwidth = bandwidth, 
             obs = obs, eff.obs = eff.obs, means = means)


columns <- c("(1)", "(2)", "(3)")

colnames(tab) <- columns

rownames(tab) <- c("Non-Cannabis Business Tax Approved", "", "Polynomial", 
                   "Bandwidth", "Observations", "Obs. in Bandwidth", "Mean Outcome")

tab <- xtable(tab, align = c("l", "c", "c", "c"))


addtorow <- list()
addtorow$pos <- list(-1,0, 2,nrow(tab))
addtorow$command <- c("\\\\[-1.8ex] \\hline \\hline \\\\[-1.8ex]  & 
                      \\multicolumn{3}{c}{Incumbent Vote Share} 
                      \\\\ \\cline{2-4} \\\\[-5ex] \\\\",
                      "\\hline \\\\[-1.8ex] ", "\\\\[-1.8ex] \\hline \\\\[-1.8ex]", 
                      "\\\\[-1.8ex] \\hline \\hline \\\\[-1.8ex]")

print(tab, add.to.row = addtorow, hline.after = NULL,
      floating = FALSE, file = "output/tableA9.tex")



########################################################################################################
# Table A.10: Effects of Business Tax Robust to Dropping Cities with Term Limits

dta <- read.csv("data/rdd-data.csv")
business <- dta[dta$reclassification == "Business Tax", ]


# drop cities with term limits
business <- business[!(business$city %in% c("anaheim", "santa ana", "san francisco", 
                                            "garden grove", "newport beach", "santa barbara",
                                            "dana point", "huntington beach")), ] 


m <- (rdrobust(business$avg.inc.voteshare, business$marg, c=0, all=T, p=1,
               cluster = business$city))

m1 <- (rdrobust(business$avg.inc.voteshare, business$marg, c=0, all=T, p=2,
                cluster = business$city))

m2 <- (rdrobust(business$avg.inc.voteshare, business$marg, c=0, all=T, p=3,
                cluster = business$city))

m3 <- (rdrobust(business$prop.inc.win, business$marg, c=0, all=T, p=1,
                cluster = business$city))

m4 <- (rdrobust(business$prop.inc.win, business$marg, c=0, all=T, p=2,
                cluster = business$city))

m5 <- (rdrobust(business$prop.inc.win, business$marg, c=0, all=T, p=3,
                cluster = business$city))

coef <- round(c(m$coef[2], m1$coef[2], m2$coef[2], m3$coef[2], m4$coef[2], m5$coef[2]), 3)

se <- c(paste0("(",round(m$se[2], 3),")"), 
        paste0("(",round(m1$se[2], 3),")"), 
        paste0("(",round(m2$se[2],3),")"), 
        paste0("(",round(m3$se[2],3),")"), 
        paste0("(",round(m4$se[2],3),")"), 
        paste0("(",round(m5$se[2],3),")"))

p <- round(c(m$pv[2], m1$pv[2], m2$pv[2], m3$pv[2], m4$pv[2], m5$pv[2]), 2)

bandwidth <- round(c(m$bws[1], m1$bws[1], m2$bws[1], m3$bws[1], m4$bws[1], m5$bws[1]), 3)


obs <- c(sum(m$N), sum(m1$N), sum(m2$N), sum(m3$N))

eff.obs <- c(sum(m$N_h), sum(m1$N_h), sum(m2$N_h), sum(m3$N_h))

means <- round(c(mean(business$avg.inc.voteshare, na.rm = T), 
                 mean(business$avg.inc.voteshare, na.rm = T), 
                 mean(business$avg.inc.voteshare, na.rm = T),
                 mean(business$prop.inc.win, na.rm = T), 
                 mean(business$prop.inc.win, na.rm = T),
                 mean(business$prop.inc.win, na.rm = T)), 2)

poly <- c(1, 2, 3, 1, 2, 3)


tab <- rbind(coef, SE = se, poly = poly, bandwidth = bandwidth, 
             obs = obs, eff.obs = eff.obs, means = means)


columns <- c("(1)", "(2)", "(3)", "(4)", "(5)", "(6)")

colnames(tab) <- columns

rownames(tab) <- c("Business Tax Approved", "", "Polynomial", "Bandwidth", 
                   "Observations", "Obs. in Bandwidth", "Mean Outcome")

tab <- xtable(tab, align = c("l", "c", "c", "c", "c", "c", "c"))


addtorow <- list()
addtorow$pos <- list(-1,0, 2,nrow(tab))
addtorow$command <- c("\\\\[-1.8ex] \\hline \\hline \\\\[-1.8ex]  & 
                      \\multicolumn{3}{c}{Incumbent Vote Share} & 
                      \\multicolumn{3}{c}{Prop. Incumbents Winning} 
                      \\\\ \\cline{2-4} \\cline{5-7} \\\\[-5ex] \\\\",
                      "\\hline \\\\[-1.8ex] ", "\\\\[-1.8ex] \\hline \\\\[-1.8ex]", 
                      "\\\\[-1.8ex] \\hline \\hline \\\\[-1.8ex]")

print(tab, add.to.row = addtorow, hline.after = NULL,
      floating = FALSE, file = "output/tableA10.tex")



########################################################################################################
# Table A.11: Effects of Business Tax on Mayoral Versus City Council Vote Share

dta <- read.csv("data/rdd-data-mayors.csv")
dta1 <- read.csv("data/rdd-data-council.csv")

business <- dta[dta$reclassification == "Business Tax", ]
business1 <- dta1[dta1$reclassification == "Business Tax", ]


# Mayoral races only
m <- (rdrobust(business$avg.inc.voteshare, business$marg, c=0, all=T, p=1,
               cluster = business$city))

m1 <- (rdrobust(business$avg.inc.voteshare, business$marg, c=0, all=T, p=2,
                cluster = business$city))

m2 <- (rdrobust(business$avg.inc.voteshare, business$marg, c=0, all=T, p=3,
                cluster = business$city))

# Council races only
m3 <- (rdrobust(business1$avg.inc.voteshare, business1$marg, c=0, all=T, p=1,
                cluster = business1$city))

m4 <- (rdrobust(business1$avg.inc.voteshare, business1$marg, c=0, all=T, p=2,
                cluster = business1$city))

m5 <- (rdrobust(business1$avg.inc.voteshare, business1$marg, c=0, all=T, p=3,
                cluster = business1$city))


coef <- round(c(m$coef[2], m1$coef[2], m2$coef[2], m3$coef[2], m4$coef[2], m5$coef[2]), 3)

se <- c(paste0("(",round(m$se[2], 3),")"), 
        paste0("(",round(m1$se[2], 3),")"), 
        paste0("(",round(m2$se[2], 3),")"), 
        paste0("(",round(m3$se[2], 3),")"), 
        paste0("(",round(m4$se[2], 3),")"), 
        paste0("(",round(m5$se[2], 3),")"))

p <- round(c(m$pv[2], m1$pv[2], m2$pv[2], m3$pv[2], m4$pv[2], m5$pv[2]), 2)

bandwidth <- round(c(m$bws[1], m1$bws[1], m2$bws[1], m3$bws[1], m4$bws[1], m5$bws[1]), 3)


obs <- c(sum(m$N), sum(m1$N), sum(m2$N))
eff.obs <- c(sum(m$N_h), sum(m1$N_h), sum(m2$N_h), sum(m3$N_h), sum(m4$N_h), sum(m5$N_h))

means <- round(c(mean(dta$avg.inc.voteshare, na.rm = T), 
                 mean(dta$avg.inc.voteshare, na.rm = T),
                 mean(dta$avg.inc.voteshare, na.rm = T),
                 mean(dta1$avg.inc.voteshare, na.rm = T), 
                 mean(dta1$avg.inc.voteshare, na.rm = T), 
                 mean(dta1$avg.inc.voteshare, na.rm = T)), 2)

poly <- c(1, 2, 3, 1, 2, 3)


tab <- rbind(coef, SE = se, poly = poly, bandwidth = bandwidth, 
             obs = obs, eff.obs = eff.obs, means = means)


columns <- c("(1)", "(2)", "(3)", "(4)", "(5)", "(6)")
colnames(tab) <- columns

rownames(tab) <- c("Tax Increase Approved", "", "Polynomial", "Bandwidth", 
                   "Observations", "Obs. in Bandwidth", "Mean Outcome")

tab <- xtable(tab, align = c("l", "c", "c", "c", "c", "c", "c"))


addtorow <- list()
addtorow$pos <- list(-1,0, 2,nrow(tab))
addtorow$command <- c("\\\\[-1.8ex] \\hline \\hline \\\\[-1.8ex]  & 
                      \\multicolumn{3}{c}{Mayoral Races} &
                      \\multicolumn{3}{c}{Council Races} 
                      \\\\ \\cline{2-4} \\cline{5-7} \\\\[-5ex] \\\\",
                      "\\hline \\\\[-1.8ex] ", "\\\\[-1.8ex] \\hline \\\\[-1.8ex]", 
                      "\\\\[-1.8ex] \\hline \\hline \\\\[-1.8ex]")

print(tab, add.to.row = addtorow, hline.after = NULL,
      floating = FALSE, file = "output/tableA11.tex")



########################################################################################################
# Table A.12: Effects of Business Tax Robust to Adjusting for Number of Seats Up For Election

dta <- read.csv("data/rdd-data.csv")
business <- dta[dta$reclassification == "Business Tax", ]

m <- (rdrobust(business$avg.inc.voteshare, business$marg, c=0, all=T, p=1,
               cluster = business$city, covs = business$num.seats))

m1 <- (rdrobust(business$avg.inc.voteshare, business$marg, c=0, all=T, p=2,
                cluster = business$city, covs = business$num.seats))

m2 <- (rdrobust(business$avg.inc.voteshare, business$marg, c=0, all=T, p=3,
                cluster = business$city, covs = business$num.seats))


coef <- round(c(m$coef[2], m1$coef[2], m2$coef[2]), 3)

se <- c(paste0("(",round(m$se[2], 3),")"), 
        paste0("(",round(m1$se[2], 3),")"), 
        paste0("(",round(m2$se[2],3),")"))

p <- round(c(m$pv[2], m1$pv[2], m2$pv[2]), 2)

bandwidth <- round(c(m$bws[1], m1$bws[1], m2$bws[1]), 3)


obs <- c(sum(m$N), sum(m1$N), sum(m2$N))
eff.obs <- c(sum(m$N_h), sum(m1$N_h), sum(m2$N_h))

means <- round(c(mean(business$avg.inc.voteshare, na.rm = T), 
                 mean(business$avg.inc.voteshare, na.rm = T), 
                 mean(business$avg.inc.voteshare, na.rm = T)), 2)

poly <- c(1, 2, 3)


tab <- rbind(coef, SE = se, poly = poly, bandwidth = bandwidth, 
             obs = obs, eff.obs = eff.obs, means = means)


columns <- c("(1)", "(2)", "(3)")

colnames(tab) <- columns

rownames(tab) <- c("Business Tax Approved", "", "Polynomial", 
                   "Bandwidth", "Observations", "Obs. in Bandwidth", "Mean Outcome")

tab <- xtable(tab, align = c("l", "c", "c", "c"))


addtorow <- list()
addtorow$pos <- list(-1,0, 2,nrow(tab))
addtorow$command <- c("\\\\[-1.8ex] \\hline \\hline \\\\[-1.8ex]  & 
                      \\multicolumn{3}{c}{Incumbent Vote Share} 
                      \\\\ \\cline{2-4} \\\\[-5ex] \\\\",
                      "\\hline \\\\[-1.8ex] ", "\\\\[-1.8ex] \\hline \\\\[-1.8ex]", 
                      "\\\\[-1.8ex] \\hline \\hline \\\\[-1.8ex]")

print(tab, add.to.row = addtorow, hline.after = NULL,
      floating = FALSE, file = "output/tableA12.tex")



########################################################################################################
## Table A.13: Timing of Punishment for Business Taxes

dta <- read.csv("data/rdd-data.csv")
business <- dta[dta$reclassification == "Business Tax", ]

m <- rdrobust(business$avg.inc.voteshare.lag, business$marg, c=0, all=T, p=1,
              cluster = business$city)

m1 <- rdrobust(business$avg.inc.voteshare.lag, business$marg, c=0, all=T, p=2,
               cluster = business$city)

m2 <- rdrobust(business$avg.inc.voteshare.lag, business$marg, c=0, all=T, p=3,
               cluster = business$city)

m3 <- rdrobust(business$change.voteshare, business$marg, c=0, all=T, p=1,
               cluster = business$city)

m4 <- rdrobust(business$change.voteshare, business$marg, c=0, all=T, p=2,
               cluster = business$city)

m5 <- rdrobust(business$change.voteshare, business$marg, c=0, all=T, p=3,
               cluster = business$city)


coef <- round(c(m$coef[2], m1$coef[2], m2$coef[2], m3$coef[2], m4$coef[2], m5$coef[2]), 3)

se <- c(paste0("(",round(m$se[2], 3),")"), 
        paste0("(",round(m1$se[2], 3),")"), 
        paste0("(",round(m2$se[2], 3),")"), 
        paste0("(",round(m3$se[2], 3),")"), 
        paste0("(",round(m4$se[2], 3),")"), 
        paste0("(",round(m5$se[2], 3),")"))

p <- round(c(m$pv[2], m1$pv[2], m2$pv[2], m3$pv[2], m4$pv[2], m5$pv[2]), 2)

bandwidth <- round(c(m$bws[1], m1$bws[1], m2$bws[1], m3$bws[1], m4$bws[1], m5$bws[1]), 3)


obs <- c(sum(m$N), sum(m1$N), sum(m2$N))
eff.obs <- c(sum(m$N_h), sum(m1$N_h), sum(m2$N_h), sum(m3$N_h), sum(m4$N_h), sum(m5$N_h))

means <- round(c(mean(business$avg.inc.voteshare, na.rm = T), 
                 mean(business$avg.inc.voteshare, na.rm = T), 
                 mean(business$avg.inc.voteshare, na.rm = T),
                 mean(business$prop.inc.win, na.rm = T), 
                 mean(business$prop.inc.win, na.rm = T),
                 mean(business$prop.inc.win, na.rm = T)), 2)

poly <- c(1, 2, 3, 1, 2, 3)


tab <- rbind(coef, SE = se, poly = poly, bandwidth = bandwidth, 
             obs = obs, eff.obs = eff.obs, means = means)


columns <- c("(1)", "(2)", "(3)", "(4)", "(5)", "(6)")

colnames(tab) <- columns

rownames(tab) <- c("Business Tax Approved", "", "Polynomial", "Bandwidth", 
                   "Observations", "Obs. in Bandwidth", "Mean Outcome")

tab <- xtable(tab, align = c("l", "c", "c", "c", "c", "c", "c"))


addtorow <- list()
addtorow$pos <- list(-1,0, 2,nrow(tab))
addtorow$command <- c("\\\\[-1.8ex] \\hline \\hline \\\\[-1.8ex]  & 
                      \\multicolumn{3}{c}{Incumbent Vote Share} & 
                      \\multicolumn{3}{c}{$\\Delta$ in Incumbent Vote Share} \\\\ &
                      \\multicolumn{3}{c}{Previous Election}
                      \\\\ \\cline{2-4} \\cline{5-7} \\\\[-5ex] \\\\",
                      "\\hline \\\\[-1.8ex] ", "\\\\[-1.8ex] \\hline \\\\[-1.8ex]", 
                      "\\\\[-1.8ex] \\hline \\hline \\\\[-1.8ex]")

print(tab, add.to.row = addtorow, hline.after = NULL,
      floating = FALSE, file = "output/tableA13.tex")



########################################################################################################
## Table A.14: Tax Increases and Electoral Punishment Within-City

dta <- read.csv("data/election-panel.csv")
dta$num.seats <- factor(dta$num.seats)

m <- felm(vote.change ~ tax.increase1 + num.seats | city + election.year | 0 | city, data = dta)

m1 <- felm(vote.change ~ business.tax.increase1 + num.seats | city + election.year | 0 | city, data = dta)

m2 <- felm(vote.change ~ property.tax.increase1 + num.seats | city + election.year | 0 | city, data = dta)

m3 <- felm(vote.change ~ sales.tax.increase1 + num.seats | city + election.year | 0 | city, data = dta)

m4 <- felm(vote.change ~ hotel.tax.increase1 + num.seats | city + election.year | 0 | city, data = dta)

m5 <- felm(vote.change ~ utility.tax.increase1 + num.seats | city + election.year | 0 | city, data = dta)


notes <- paste("\\parbox[t]{.9\\textwidth}{Models adjust for number of contested seats",
               "in each election. Robust standard errors clustered by city. $^{*}$p$<$0.05}")  

stargazer(m, m1, m2, m3, m4, m5, type = "text", star.cutoffs = c(0.05),
          keep.stat = c("adj.rsq", "n"), 
          out = "output/tableA14.tex",
          table.placement = "tb", label = "tab:voteshare_panel",
          title = c("\\textbf{Tax Increases and Electoral Punishment Within-City}"),
          dep.var.caption = "Change Incumbent Vote Share",
          dep.var.labels.include = FALSE, font.size = "small", 
          keep = c("tax.increase1", "business.tax.increase1", "sales.tax.increase1", 
                   "property.tax.increase1", "transient.tax.increase1", "utility.tax.increase1"),
          covariate.labels = c("Any Tax", "Business Tax", "Property Tax", 
                               "Sales Tax", "Transient Occupancy Tax", "Utility Tax",
                               "Any Tax"),
          add.lines = list(c("City + Year FEs", rep("Yes", 6))),
          notes.append = FALSE, notes.align = "l", notes.label  = "",
          notes = notes)


########################################################################################################
# Figure A.4: Effect of Business Tax Increase on Incumbent Vote Share in Next Election

dta <- read.csv("data/rdd-data.csv")
business <- dta[dta$reclassification == "Business Tax", ]

m <- (rdrobust(business$avg.inc.voteshare, business$marg, c=0, all=T, p=1,
               cluster = business$city))


plot <- rdplot(business$avg.inc.voteshare, business$marg, binselect = "qs", # x.lim = c(-.3, .3),
               y.lim = c(0, .75), x.lim = c(-.4, .4),
               x.label = "Vote Share Margin for Business Tax Increase", 
               y.label = "Incumbent Vote Share", 
               col.lines = "black", title = "")

(p <- plot$rdplot + geom_point(aes(x=business$marg, y = business$avg.inc.voteshare), alpha = .2) +
    theme_bw(base_family = "serif", base_size = 12))

ggsave("output/figureA4.pdf", plot = p,
       width = 6, height = 4, units = "in", device = "pdf")



########################################################################################################
## Table A.15: Effects of Tax Increases on Number of Business Owners Running

dta <- read.csv("data/rdd-data.csv")

business <- dta[dta$reclassification=="Business Tax",]
sales <- dta[dta$reclassification=="Sales Tax",]
property <- dta[dta$reclassification=="Property Tax",]
utility <- dta[dta$reclassification=="Utility Tax",]
hotel <- dta[dta$reclassification=="Transient Occupancy Tax",]

m <- (rdrobust(dta$business.owners, dta$marg, c=0, all=T, p=1,
               cluster = dta$city))

m1 <- (rdrobust(business$business.owners, business$marg, c=0, all=T, p=1, 
                cluster = business$city))

m2 <- (rdrobust(property$business.owners, property$marg, c=0, all=T, p=1,
                cluster = property$city))

m3 <- (rdrobust(sales$business.owners, sales$marg, c=0, all=T, p=1,
                cluster = sales$city))

m4 <- (rdrobust(hotel$business.owners, hotel$marg, c=0, all=T, p=1,
                cluster = hotel$city))

m5 <- (rdrobust(utility$business.owners, utility$marg, c=0, all=T, p=1,
                cluster = utility$city))


overall.effect <- c(round(m$coef[2], 3), "", "", "", "", "")
overall.se <- c(paste0("(",round(m$se[2], 3),")"), "", "", "", "", "")

business.effect <- c("", round(m1$coef[2], 3), "", "", "", "")
business.se <- c("", paste0("(",round(m1$se[2], 3),")"),  "", "", "", "")

property.effect <- c("", "", round(m2$coef[2], 3), "", "", "")
property.se <- c("", "", paste0("(",round(m2$se[2], 3),")"),  "", "", "")

sales.effect <- c("", "", "", round(m3$coef[2], 3), "", "")
sales.se <- c("", "", "", paste0("(",round(m3$se[2], 3),")"), "", "")

hotel.effect <- c("", "", "", "", round(m4$coef[2], 3), "")
hotel.se <- c("", "", "", "", paste0("(",round(m4$se[2], 3),")"), "")

utility.effect <- c("", "", "", "", "", round(m5$coef[2], 3))
utility.se <- c("", "", "", "", "", paste0("(",round(m5$se[2], 3),")"))


bandwidth <- round(c(m$bws[1], m1$bws[1], m2$bws[1], m3$bws[1], m4$bws[1], m5$bws[1]), 3)

obs <- c(sum(m$N), sum(m1$N), sum(m2$N))
eff.obs <- c(sum(m$N_h), sum(m1$N_h), sum(m2$N_h), sum(m3$N_h), sum(m4$N_h), sum(m5$N_h))

means <- round(c(mean(dta$business.owners, na.rm = T), 
                 mean(business$business.owners, na.rm = T), 
                 mean(property$business.owners, na.rm = T),
                 mean(sales$business.owners, na.rm = T), 
                 mean(hotel$business.owners, na.rm = T),
                 mean(utility$business.owners, na.rm = T)), 2)

poly <- rep(1, 6)

tab <- rbind(overall.effect = overall.effect, overall.se = overall.se,
             business.effect = business.effect, business.se = business.se,
             property.effect = property.effect, property.se = property.se,
             sales.effect = sales.effect, sales.se = sales.se, 
             hotel.effect = hotel.effect, hotel.se = hotel.se,
             utility.effect = utility.effect, utility.se = utility.se,
             bandwidth = bandwidth, obs = obs, eff.obs = eff.obs, means = means)


columns <- c("(1)", "(2)", "(3)", "(4)", "(5)", "(6)")

colnames(tab) <- columns

rownames(tab) <- c("Any Tax", "",
                   "Business Tax", " ",
                   "Property Tax", "  ",
                   "Sales Tax", "   ",
                   "Hotel Tax", "    ",
                   "Utility Tax", "     ",
                   "Bandwidth", "Observations", "Obs. in Bandwidth", "Mean Outcome")

tab <- xtable(tab, align = c("l", "c", "c", "c", "c", "c", "c"))


addtorow <- list()
addtorow$pos <- list(-1,0, 12,nrow(tab))
addtorow$command <- c("\\\\[-1.8ex] \\hline \\hline \\\\[-1.8ex]  & 
                      \\multicolumn{6}{c}{Number of Business Owners Running} 
                      \\\\ \\cline{2-4} \\cline{5-7} \\\\[-5ex] \\\\",
                      "\\hline \\\\[-1.8ex] ", "\\\\[-1.8ex] \\hline \\\\[-1.8ex]", "\\\\[-1.8ex] \\hline \\hline \\\\[-1.8ex]")


print(tab, add.to.row = addtorow, hline.after = NULL,
      floating = FALSE, file = "output/tableA15.tex")



########################################################################################################
## Figure A.5a: Effect of Tax Increases on Votes Cast in Next Election

# (a) Effect on Total Votes (Logged)
dta <- read.csv("data/rdd-data.csv")
dta$log.votes <- log(dta$totvotes)

m <- (rdrobust(dta$log.votes, dta$marg, c=0, all=T, cluster = dta$city, p = 1))

business <- dta[dta$reclassification =="Business Tax",]
m1 <- (rdrobust(business$log.votes, business$marg, c=0, all=T, cluster = business$city, p = 1))

property <- dta[dta$reclassification=="Property Tax",]
m2 <- (rdrobust(property$log.votes, property$marg, c=0, all=T, cluster = property$city, p = 1))

sales <- dta[dta$reclassification=="Sales Tax",]
m3 <- (rdrobust(sales$log.votes, sales$marg, c=0, all=T, cluster = sales$city, p = 1))

utility <- dta[dta$reclassification=="Utility Tax",]
m4 <- (rdrobust(utility$log.votes, utility$marg, c=0, all=T, cluster = utility$city, p = 1))

hotel <- dta[dta$reclassification=="Transient Occupancy Tax",]
m5 <- (rdrobust(hotel$log.votes, hotel$marg, c=0, all=T, cluster = hotel$city, p = 1))


N <- c(sum(m$N), sum(m1$N), sum(m2$N), sum(m3$N), sum(m4$N), sum(m5$N))

modelcoef <- (c(m$coef[2], m1$coef[2], m2$coef[2], m3$coef[2], m4$coef[2], m5$coef[2]))

modelse <- c(m$se[2], m1$se[2], m2$se[2], m3$se[2], m4$se[2], m5$se[2])


ylo = modelcoef - qt(.975, N)*(modelse)
yhi = modelcoef + qt(.975, N)*(modelse)

names = c("Any Tax Increase", "Business Tax Increase", "Property Tax Increase", 
          "Sales Tax Increase", "Utility Tax Increase", "Transient Occupancy Tax Increase")

dfplot = data.frame(names, modelcoef, modelse, ylo, yhi)

(p <- ggplot(dfplot, aes(names, modelcoef, ymin=ylo, ymax=yhi)) +
    geom_pointrange(size = .25) + 
    coord_flip() + geom_hline(yintercept = 0, lty=2) + xlab("") + ylab("") +
    theme_bw(base_family = "serif", base_size = 15)  +
    theme(plot.background = element_blank(),
          panel.grid.major = element_blank(),
          panel.grid.minor = element_blank()))

ggsave("output/figureA5a.pdf", plot = p,
       width = 5, height = 3.5, units = "in", device = "pdf")


########################################################################################################
## Figure A.5b: Effect of Tax Increases on Votes Cast in Next Election

# (b) Effect on Votes Per Seat
dta <- read.csv("data/rdd-data.csv")

dta <- dta %>% 
  mutate(log.avg.tot.votes.per.seat = log(totvotes/num.seats))

m <- (rdrobust(dta$log.avg.tot.votes.per.seat, dta$marg, c=0, all=T, cluster = dta$city, p = 1))

business <- dta[dta$reclassification =="Business Tax",]
m1 <- (rdrobust(business$log.avg.tot.votes.per.seat, business$marg, c=0, all=T, cluster = business$city, p = 1))

property <- dta[dta$reclassification=="Property Tax",]
m2 <- (rdrobust(property$log.avg.tot.votes.per.seat, property$marg, c=0, all=T, cluster = property$city, p = 1))

sales <- dta[dta$reclassification=="Sales Tax",]
m3 <- (rdrobust(sales$log.avg.tot.votes.per.seat, sales$marg, c=0, all=T, cluster = sales$city, p = 1))

utility <- dta[dta$reclassification=="Utility Tax",]
m4 <- (rdrobust(utility$log.avg.tot.votes.per.seat, utility$marg, c=0, all=T, cluster = utility$city, p = 1))

hotel <- dta[dta$reclassification=="Transient Occupancy Tax",]
m5 <- (rdrobust(hotel$log.avg.tot.votes.per.seat, hotel$marg, c=0, all=T, cluster = hotel$city, p = 1))


N <- c(sum(m$N), sum(m1$N), sum(m2$N), sum(m3$N), sum(m4$N), sum(m5$N))

modelcoef <- (c(m$coef[2], m1$coef[2], m2$coef[2], m3$coef[2], m4$coef[2], m5$coef[2]))

modelse <- c(m$se[2], m1$se[2], m2$se[2], m3$se[2], m4$se[2], m5$se[2])


ylo = modelcoef - qt(.975, N)*(modelse)
yhi = modelcoef + qt(.975, N)*(modelse)

names = c("Any Tax Increase", "Business Tax Increase", "Property Tax Increase", 
          "Sales Tax Increase", "Utility Tax Increase", "Transient Occupancy Tax Increase")

dfplot = data.frame(names, modelcoef, modelse, ylo, yhi)

(p <- ggplot(dfplot, aes(names, modelcoef, ymin=ylo, ymax=yhi)) +
    geom_pointrange(size = .25) + 
    coord_flip() + geom_hline(yintercept = 0, lty=2) + xlab("") + ylab("") +
    theme_bw(base_family = "serif", base_size = 15)  +
    theme(plot.background = element_blank(),
          panel.grid.major = element_blank(),
          panel.grid.minor = element_blank()))

ggsave("output/figureA5b.pdf", plot = p,
       width = 5, height = 3.5, units = "in", device = "pdf")


########################################################################################################
## Table A.16: Effect of Business Tax Increases on Votes Cast in Next Election

dta <- read.csv("data/rdd-data.csv")
business <- dta[dta$reclassification == "Business Tax", ]

m <- (rdrobust(business$log.votes, business$marg, c=0, all=T, p=1,
               cluster = business$city))

m1 <- (rdrobust(business$log.votes, business$marg, c=0, all=T, p=2,
                cluster = business$city))

m2 <- (rdrobust(business$log.votes, business$marg, c=0, all=T, p=3,
                cluster = business$city))

coef <- round(c(m$coef[2], m1$coef[2], m2$coef[2]), 3)

se <- c(paste0("(",round(m$se[2], 3),")"), 
        paste0("(",round(m1$se[2], 3),")"), 
        paste0("(",round(m2$se[2],3),")"))

p <- round(c(m$pv[2], m1$pv[2], m2$pv[2]), 2)

bandwidth <- round(c(m$bws[1], m1$bws[1], m2$bws[1]), 3)

obs <- c(sum(m$N), sum(m1$N), sum(m2$N))
eff.obs <- c(sum(m$N_h), sum(m1$N_h), sum(m2$N_h))

means <- round(c(mean(business$log.votes, na.rm = T), 
                 mean(business$log.votes, na.rm = T), 
                 mean(business$log.votes, na.rm = T)), 2)

poly <- c(1, 2, 3)


tab <- rbind(coef, SE = se, poly = poly, bandwidth = bandwidth, 
             obs = obs, eff.obs = eff.obs, means = means)


columns <- c("(1)", "(2)", "(3)")

colnames(tab) <- columns

rownames(tab) <- c("Business Tax Approved", "", "Polynomial", "Bandwidth", 
                   "Observations", "Obs. in Bandwidth", "Mean Outcome")

tab <- xtable(tab, align = c("l", "c", "c", "c"))


addtorow <- list()
addtorow$pos <- list(-1,0, 2,nrow(tab))
addtorow$command <- c("\\\\[-1.8ex] \\hline \\hline \\\\[-1.8ex]  & 
                      \\multicolumn{3}{c}{Votes in Next Election (Log)} 
                      \\\\ \\cline{2-4}  \\\\[-5ex] \\\\",
                      "\\hline \\\\[-1.8ex] ", "\\\\[-1.8ex] \\hline \\\\[-1.8ex]", 
                      "\\\\[-1.8ex] \\hline \\hline \\\\[-1.8ex]")

print(tab, add.to.row = addtorow, hline.after = NULL,
      floating = FALSE, file = "output/tableA16.tex")
